﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.OracleClient;
using Xiaov.SmartWeb.Application.Models;

namespace Xiaov.SmartWeb.Application.SZBBTree.NH08
{
  public partial class SaveTable : System.Web.UI.Page
  {
    private string tableName = "", dwbm = "", nf = "", yf = "", xml = "";
    XMLVERSION modelXml = null;
    protected void Page_Load(object sender, EventArgs e)
    {
      string jsonData = "";
      if (Request.Form["tableName"] != null || Request.Form["dwbm"] != null ||
        Request.Form["nf"] != null || Request.Form["yf"] != null)
      {
        tableName = Request.Form["tableName"].ToString();
        dwbm = Request.Form["dwbm"].ToString();
        nf = Request.Form["nf"].ToString();
        yf = Request.Form["yf"].ToString();
        xml = Server.UrlDecode(Request.Form["xml"].ToString());
        //InitVersion();
        modelXml = new XMLVERSION(nf, yf, dwbm, tableName);
        if (Save())
        { jsonData = "{\"save\":1}"; }
        else
        { jsonData = "{\"save\":0}"; }
      }
      else
      {
        jsonData = "{\"save\":0}";
      }
      Response.Write(jsonData);
      Response.ContentType = "text/plain";
      Response.End();


    }

    private bool Save()
    {
      bool flag = false;
      System.Text.StringBuilder sql = new System.Text.StringBuilder();
      sql.Append("begin ");
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(xml);
      XmlNode worksheet = doc.DocumentElement;
      foreach (XmlNode row in worksheet.ChildNodes)
      {
        string[] name = row.Attributes["name"].Value.ToString().Split(new char[] { '_' });
        string zbbm = name[0];
        string colName = name[1];
        string value = row.InnerText;
        sql.Append("update "); sql.Append(tableName); sql.Append(" set ");
        sql.Append(colName); sql.Append("='"); sql.Append(value); sql.Append("' ");
        sql.Append(" where dwbm='"); sql.Append(dwbm);
        sql.Append("' and nf='"); sql.Append(nf);
        sql.Append("' and yf='"); sql.Append(yf);
        sql.Append("' and zbbm='"); sql.Append(zbbm);
        sql.Append("';");
      }
      sql.Append("null;");
      sql.Append("end;");
      //string s = sql.ToString();
      flag = DbHelperOra.ExecuteSql(sql.ToString()) > 0 ? true : false; ;
      return flag;
    }
  }
}